<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    v-if="dialogVisible"
    :close-on-click-modal="false"
    width="30%"
    :before-close="handleClose"
  >
    <div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- <el-form-item
          label="邮箱"
          prop="email"
          :rules="[
            { required: true, message: '请输入邮箱地址', trigger: 'blur' },
            {
              type: 'email',
              message: '请输入正确的邮箱地址',
              trigger: ['blur', 'change'],
            },
          ]"
        >
          <el-input v-model="ruleForm.email">
             <template slot="append">@b1b.com</template>
          </el-input>
        </el-form-item> -->
        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="ruleForm.email"
            @keyup.enter.native="submitForm('ruleForm')"
            v-focus
          >
            <template slot="append">@b1b.com</template>
          </el-input>
        </el-form-item>

        <el-form-item label="真实姓名" prop="realName">
          <el-input
            v-model="ruleForm.realName"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span class="dialog-footer">
      <el-button @click="resetForm('ruleForm')">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      title: "新增邮箱",
      dialogVisible: false,
      ruleForm: {
        email: "",
        realName: "",
      },
      rules: {
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
        realName: [
          { required: true, message: "请输入真实名字", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    open(data) {
      this.title = "新增邮箱";
      if (data) {
        this.title = "编辑邮箱";
        this.ruleForm = { ...data };
        this.ruleForm.email = this.ruleForm.email.replace(/@b1b.com/g, "");
      }
      this.dialogVisible = true;
    },
    handleClose() {
      this.resetForm("ruleForm");
    },
    close() {
      this.resetForm("ruleForm");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // this.ruleForm.email += "@b1b.com";
          this.$emit("save", {...this.ruleForm});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.ruleForm = {
        email: "",
        realName: "",
      };
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },
  },
  created() {},
};
</script>
<style lang="scss" scoped>
.dialog-footer {
  display: flex;
  justify-content: center;
}
</style>
